<template>
  <a-card :bordered="false" :body-style="{'padding-top':'2px', 'padding-left':'0px', 'padding-right':'0px', 'padding-bottom':'0px'}">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper" :hidden="pageSearchHidden">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :span="8" style="height: 48px">
            <a-form-item label="编码">
              <a-input placeholder="请输入工作中心编码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8" style="height: 48px">
            <a-form-item label="名称">
              <a-input placeholder="请输入工作中心名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="8" style="height: 48px">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('工作中心')">导出</a-button>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mdm-work-center-modal ref="modalForm" @ok="modalFormOk" :init-add-model="initAddModel"></mdm-work-center-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MdmWorkCenterModal from './modules/MdmWorkCenterModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {deleteAction, getAction} from "@/api/manage";

  export default {
    name: 'MdmWorkCenterList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      MdmWorkCenterModal
    },
    data () {
      return {
        description: '工作中心管理页面',
        ipagination:{
          current: 1,
          pageSize: 5,
          pageSizeOptions: ['5'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:50,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'编码',
            align:"center",
            width:100,
            dataIndex: 'code'
          },
          {
            title:'名称',
            align:"center",
            width:120,
            dataIndex: 'name'
          },
          {
            title:'编制人数',
            align:"center",
            width:60,
            dataIndex: 'postNum'
          },
          {
            title:'备注',
            align:"center",
            width:120,
            dataIndex: 'note'
          },
          {
            title:'启用',
            align:"center",
            width:60,
            dataIndex: 'enableFlag',
            customRender: (text) => (text ? filterMultiDictText(this.dictOptions['enableFlag'], text) : ''),
          },
          {
            title:'所在位置',
            align:"center",
            width:120,
            dataIndex: 'location'
          },
          {
            title:'操作范围',
            align:"center",
            width:120,
            dataIndex: 'workScope_dictText'
          },
          {
            title:'排序',
            align:"center",
            width:60,
            dataIndex: 'sort'
          },
          {
            title:'工厂',
            align:"center",
            width:160,
            dataIndex: 'factoryId_dictText'
          },
          {
            title:'产品线',
            align:"center",
            width:160,
            dataIndex: 'productLineId_dictText'
          },
          {
            title:'公司',
            align:"center",
            width:160,
            dataIndex: 'companyId_dictText'
          },
          {
            title:'生效日期',
            align:"center",
            width:100,
            dataIndex: 'effectiveDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'失效日期',
            align:"center",
            width:100,
            dataIndex: 'expireDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/mdm/workCenter/list",
          delete: "/mdm/workCenter/delete",
          deleteBatch: "/mdm/workCenter/deleteBatch",
          exportXlsUrl: "/mdm/workCenter/exportXls",
          importExcelUrl: "mdm/workCenter/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    props:{
      pageSearchHidden: false,
      initQueryParam:{},
      initAddModel:{},
    },
    created() {
      this.$set(this.dictOptions, 'partOrderFlag', [{text:'是',value:'1'},{text:'否',value:'0'}])
      this.$set(this.dictOptions, 'repairOrderFlag', [{text:'是',value:'1'},{text:'否',value:'0'}])
      this.$set(this.dictOptions, 'enableFlag', [{text:'是',value:'1'},{text:'否',value:'0'}])
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    watch:{
      initQueryParam: {
        deep: true,
        handler(newVal, oldVal){
          if(newVal.pid !== oldVal.pid){
            this.loadData();
          }
        }
      }
    },
    methods: {
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        if(Object.keys(this.initQueryParam).find(key => !this.initQueryParam[key])){
          return;
        }
        Object.assign(this.queryParam, this.initQueryParam);
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      batchDel: function () {
        if(!this.url.deleteBatch){
          this.$message.error("请设置url.deleteBatch属性!")
          return
        }
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认删除",
            content: "是否删除选中数据?",
            onOk: function () {
              that.loading = true;
              deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
                if (res.success) {
                  //重新计算分页问题
                  that.reCalculatePage(that.selectedRowKeys.length)
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                  that.$emit('ok');
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      handleDelete: function (id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(1)
            that.$message.success(res.message);
            that.loadData();
            that.$emit('ok');
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handleAdd: function () {
        this.$refs.modalForm.add(this.initAddModel.pid);
        this.$refs.modalForm.title = "新增";
        this.$refs.modalForm.disableSubmit = false;
      },
      modalFormOk() {
        // 新增/修改 成功时，重载列表
        this.loadData();
        //清空列表选中
        this.onClearSelected()
        this.$emit('ok')
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'code',text:'编码',dictCode:''})
        fieldList.push({type:'string',value:'name',text:'名称',dictCode:''})
        fieldList.push({type:'string',value:'shortName',text:'简称',dictCode:''})
        fieldList.push({type:'string',value:'factoryId',text:'工厂',dictCode:'mdm_factory,factory_name,id'})
        fieldList.push({type:'string',value:'companyId',text:'公司',dictCode:'sys_depart,depart_name,id'})
        fieldList.push({type:'int',value:'postNum',text:'编制人数',dictCode:''})
        fieldList.push({type:'string',value:'workScope',text:'操作范围',dictCode:'mdm_work_scope'})
        fieldList.push({type:'string',value:'location',text:'所在位置',dictCode:''})
        fieldList.push({type:'int',value:'partDays',text:'零配件提取天数',dictCode:''})
        fieldList.push({type:'switch',value:'partOrderFlag',text:'零配件下工单'})
        fieldList.push({type:'switch',value:'repairOrderFlag',text:'检修计划下工单'})
        fieldList.push({type:'string',value:'scanWay',text:'扫码方式',dictCode:'scan_way'})
        fieldList.push({type:'date',value:'effectiveDate',text:'生效日期'})
        fieldList.push({type:'date',value:'expireDate',text:'失效日期'})
        fieldList.push({type:'int',value:'sort',text:'排序',dictCode:''})
        fieldList.push({type:'string',value:'note',text:'备注',dictCode:''})
        fieldList.push({type:'switch',value:'enable',text:'启用'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>